package com.alogic.es.xscript;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;


/**
 * 指令请求
 *
 * @since 1.6.13.9 [20200824 duanyy]
 */
public class EsCommandOnRequest extends Segment {
    protected String callbackId = "$es-callback-request";

    public EsCommandOnRequest(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        callbackId = PropertiesConstants.getRaw(p,"id",callbackId);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        Logiclet callback = ctx.getObject(callbackId);
        if (callback != null){
            callback.execute(root, current, ctx, watcher);
        }else{
            super.onExecute(root,current,ctx,watcher);
        }
    }
}
